﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyDMS.BusinessLayer;

public partial class ProjectTasks : PageBaseClass
{
    UserFolderFactory uFolderFactory;
    int projectCode;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["pcode"] != null && Request["pcode"] != "")
        {
            projectCode = int.Parse(Request["pcode"]);
        }

        if (!IsPostBack)
        {
            BindProjects();
            BindTasks();
        }
    }

    private void BindProjects()
    {
        object dataObject;
        new SqlFactory().ExecuteQuery("select p.* from Project p " +
            " inner join UserInProject uip on p.Project_Code = uip.Project_Code " +
            " where uip.User_Code = " + _author.user_Code, out dataObject, SqlFactory.DataObjectType.DataSet);

        if (dataObject != null)
        {
            ddlMyProjects.DataSource = dataObject;
            ddlMyProjects.DataTextField = "Project_Title";
            ddlMyProjects.DataValueField = "Project_Code";
            ddlMyProjects.DataBind();

            ddlMyProjects.Items.Insert(0, new ListItem("All", "0"));

            if (projectCode > 0)
            {
                ddlMyProjects.Text = projectCode.ToString();
            }
        }
    }

    private void BindTasks()
    {
        List<ProjectTask> lstProjTask = new List<ProjectTask>();

        if (projectCode > 0)
        {
            foreach (ProjectTask pt in new ProjectTaskFactory().GetAllBy(ProjectTask.ProjectTaskFields.Project_Code, projectCode))
            {
                if (pt.Assigned_To == _author.user_Code)
                {
                    lstProjTask.Add(pt);
                }
            }
        }
        else
        {
            lstProjTask = new ProjectTaskFactory().GetAllBy(ProjectTask.ProjectTaskFields.Assigned_To, _author.user_Code);
        }

        gvProjectTasks.DataSource = lstProjTask;
        gvProjectTasks.DataBind();
    }

    protected void ddlMyProjects_SelectedIndexChanged(object sender, EventArgs e)
    {
        projectCode = int.Parse(ddlMyProjects.SelectedValue);
        BindTasks();
    }

    protected void btnCloseTask_Click(object sender, EventArgs e)
    {
        if (new SqlFactory().ExecuteNonQuery("update ProjectTask set Closed_Date = " + DateTime.Now.ToString() +
             ", Closed_By = " + _author.user_Code))
        {
            lblStatus.Text = "Closed Successfully ";
        }
        else
        {
            lblStatus.Text = "Not closed";
        }
    }
}
